import requests


def senf_pdf(save_name: str):
    """
    用于发送生成PDF报告至企业微信群，
    需传入PDF报告文件名字符串参数
    :param save_name: 文件名
    :return req: 返回requests请求
    """
    key = '0e4bbb7f-56c6-442f-a367-23cec3a2e6a9'
    id_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={key}&type=file&type=file"
    wx_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}&type=file"

    data = {'file': open("./" + save_name, 'rb')}  # post jason
    response = requests.post(url=id_url, files=data)  # post 请求上传文件
    json_res = response.json()  # 返回转为json
    print(json_res)

    media_id = json_res['media_id']  # 提取返回ID
    data = {"msgtype": "file", "file": {"media_id": media_id}}  # post json
    headers = {"Content-Type": "application/json"}
    req = requests.post(wx_url, headers=headers, json=data)

    return req


if __name__ == "__main__":
    import datetime
    today = datetime.date.today()
    path = f'../../output/{today}申万一级行业日度更新.xlsx'
    senf_pdf(path)